{% extends "explorer/base.html" %}
{% load i18n %}

{% block head_title %}{% trans "SQL Query History" %}{% endblock head_title %}

{% block sql_explorer_navlinks %}
  {% if can_change %}
    <li><a href="{% url "query_create" %}">{% trans "New Query" %}</a></li>
    <li><a href="{% url "explorer_playground" %}">{% trans "Playground" %}</a></li>
    <li class="active"><a href="">{% trans "Logs" %}</a></li>
  {% endif %}
{% endblock %}

{% block sql_explorer_content %}
  <h3>
    {% blocktrans with page_num=page_obj.number %}
      Recent Query Logs - Page {{ page_num }}
    {% endblocktrans %}
  </h3>
  <div class="table-responsive">
    <table class="table table-striped query-list">
      <thead>
        <tr>
          <th>{% trans "Run At" %}</th>
          <th>{% trans "Run By" %}</th>
          <th>{% trans "Duration" %}</th>
          <th class="sql">{% trans "SQL" %}</th>
          <th>{% trans "Query ID" %}</th>
          <th>{% trans "Playground" %}</th>
        </tr>
      </thead>
      <tbody>
        {% for object in recent_logs %}
          <tr>
            <td>{{ object.run_at|date:"SHORT_DATETIME_FORMAT" }}</td>
            <td>{{ object.run_by_user.email }}</td>
            <td>{{ object.duration|floatformat:2 }}ms</td>
            <td class="log-sql">{{ object.sql }}</td>
            <td> {% if object.query_id %}<a href="{% url "query_detail" object.query_id %}">Query {{ object.query_id }}</a>{% elif object.is_playground %}Playground{% else %}--{% endif %}</td>
            <td><a href="{% url "explorer_playground" %}?querylog_id={{ object.id }}">Open</a></td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% if is_paginated %}
    <div class="pagination">
      <span class="page-links">
        {% if page_obj.has_previous %}
          <a href="?page={{ page_obj.previous_page_number }}"><<</a>
        {% endif %}
        <span class="page-current">
          {% blocktrans with page_num=page_obj.number total_pages=page_obj.paginator.num_pages %}
            Page {{ page_num }} of {{ total_pages }}.
          {% endblocktrans %}
        </span>
        {% if page_obj.has_next %}
          <a href="?page={{ page_obj.next_page_number }}">>></a>
        {% endif %}
      </span>
    </div>
  {% endif %}
{% endblock %}
